import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.List;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;


public class GUI_dest extends JPanel{
	String dest=null;
	GUI_destContext Context;
	public GUI_dest(){
		setLayout(null);
		
		GUI_destBar DestBar = new GUI_destBar();
		DestBar.setBounds(0, 0, 800, 50);
		add(DestBar);
		
		Context = new GUI_destContext();
		Context.setBounds(0, 50, 800, 420);
		add(Context);
		
	}
	
}


class GUI_destBar extends JPanel implements ActionListener{
	Image img = null;
	
	public GUI_destBar(){
		Toolkit kit = Toolkit.getDefaultToolkit(); 
		img = kit.getImage("images/bar.jpg");
		
		setLayout(null);

		JLabel lb = new JLabel("목적지 설정");
		lb.setBounds(310, 10, 200, 30);
		lb.setForeground(Color.white);
		lb.setFont(new Font("bold",Font.BOLD,30));
		add(lb);
		
		Image btimg_menu[] = new Image[4];
		btimg_menu[0] = kit.getImage("images/bt_menu.jpg"); 
		btimg_menu[1] = kit.getImage("images/bt_menu.jpg"); 
		btimg_menu[2] = kit.getImage("images/bt_menu.jpg"); 
		btimg_menu[3] = kit.getImage("images/bt_menu.jpg"); 
		
		ImageButton bt_menu = new ImageButton(btimg_menu);
		bt_menu.setBounds(700, 0, 70, 100);
		bt_menu.addActionListener(this);
		add(bt_menu);
				
	}
	protected void paintComponent(Graphics g){ 
		g.drawImage(img, 0, 0, 800, 100, this); 
	}
	public void actionPerformed(ActionEvent ae){
		Main.Navi.con.removeAll();
		
		GUI_menu Menu = new GUI_menu();
		Menu.setBounds(0, 0, 800, 100);
		Main.Navi.con.add(Menu);
		
		Main.Navi.con.validate();
	}
}


class GUI_destContext extends JPanel{
	GUI_search Search;
	GUI_last Last;
	public GUI_destContext(){
		setLayout(null);
		
		Search = new GUI_search();
		Search.setBounds(0, 0, 500, 450);
		add(Search);
		
		Last = new GUI_last();
		Last.setBounds(500, 0, 300, 450);
		add(Last);	
	}	
}


class GUI_search extends JPanel implements ItemListener{
	String dest=null;
	
	public GUI_search(){
		setLayout(null);
		
		JPanel search = new JPanel();
		search.setLayout(null);
		search.setBounds(0, 0, 500, 80);
		search.setBackground(Color.darkGray);
		
		TextField searchText = new TextField();
		searchText.setBounds(10, 20, 350, 40);
		searchText.setFont(new Font("",Font.PLAIN,25));
		searchText.setText(null);
		search.add(searchText);
		
		
		Toolkit kit = Toolkit.getDefaultToolkit(); 
		Image btimg_search[] = new Image[4];
		btimg_search[0] = kit.getImage("images/bt_search.jpg"); 
		btimg_search[1] = kit.getImage("images/bt_search.jpg"); 
		btimg_search[2] = kit.getImage("images/bt_search.jpg"); 
		btimg_search[3] = kit.getImage("images/bt_search.jpg"); 
		
		ImageButton bt_search = new ImageButton(btimg_search);
		bt_search.setBounds(370, 20, 100, 50);
		search.add(bt_search);
		
		
			
		JPanel result = new JPanel();
		result.setLayout(null);
		result.setBounds(0, 80, 500, 370);
		result.setBackground(Color.darkGray);
		
		List list = new List(10,false);
		list.setBounds(10, 20, 460, 300);
		list.setFont(new Font("",Font.PLAIN,20));
		list.addItemListener(this);
		result.add(list);
		
		SearchHandler searchHandler=new SearchHandler(searchText,list);
		bt_search.addActionListener(searchHandler);
		
		
		
		add(search);
		add(result);
		
	}

	public void itemStateChanged(ItemEvent ie){
		//紐⑹��吏� ��ㅼ��
		List list = (List)ie.getSource();
		dest = list.getSelectedItem();
		System.out.print(dest+"(이)가 목적지로 설정되었습니다.\n");
		Main.Navi.setDest(dest);
		
		//理�洹쇰━��ㅽ�몄�� 異�媛�
		Main.Navi.addDest(dest);
		
		//湲몄����댄��硫� ������
		Main.Navi.con.removeAll();		
		GUI_navi Navi = new GUI_navi("all");
		Navi.setBounds(0, 0, 800, 500);
		Main.Navi.con.add(Navi);
		Main.Navi.con.validate();
	}
}


//寃����踰���� ��몃�ㅻ��
class SearchHandler implements ActionListener
{
	TextField t;
	List list;
	
	public SearchHandler(TextField t, List list)
	{
		this.t=t;
		this.list=list;
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		ArrayList<Building> BL = new ArrayList<Building>();
		BL = Main.Navi.Search(t.getText());
		
		//��댁�� 寃����寃곌낵 珥�湲고��
		list.removeAll();
		
		//寃����寃곌낵瑜� 由ъ�ㅽ�몄�� 異�媛�
		for(int i=0;i<BL.size();i++)
			list.add(BL.get(i).getName()); 

		
		list.setBounds(10, 150, 460, 300);
		Main.Navi.con.add(list);
		
	}
}



//��댁��-理�洹쇰ぉ���吏�
class GUI_last extends JPanel implements ItemListener{
	String dest=null;
	List list;
		
	public GUI_last(){
		setLayout(null);
		setBackground(Color.darkGray);
		

		JLabel lb_navi = new JLabel("최근목적지");
		lb_navi.setBounds(10, 50, 200, 30);
		lb_navi.setFont(new Font("",Font.PLAIN,28));
		lb_navi.setForeground(Color.white);
		add(lb_navi);
		
		
		list = new List(10,false);
		list.setBounds(10, 100, 270, 300);
		list.setFont(new Font("",Font.PLAIN,20));
		list.addItemListener(this);
				
		//理�洹� 紐⑹��吏�瑜� 由ъ�ㅽ�몄�� 異�媛�
		int i = Main.Navi.getLatelyDest().size();
		for(int j=i;j>i-10&&j>0;j--)
			list.add(Main.Navi.getLatelyDest().get(j-1).getName());
		
		add(list);
	}
	
	public void itemAdd(String str){
		list.add(str);
	}
	
	public void itemStateChanged(ItemEvent ie){
		//紐⑹��吏� ��ㅼ��
		List a = (List)ie.getSource();
		dest = a.getSelectedItem();
		System.out.print(dest+"(이)가 목적지로 설정되었습니다.\n");
		Main.Navi.setDest(dest);
		//湲몄����댄��硫� ������
		Main.Navi.con.removeAll();
		
		GUI_navi Navi = new GUI_navi("all");
		Navi.setBounds(0, 0, 800, 500);
		Main.Navi.con.add(Navi);
		
		Main.Navi.con.validate();
	}
}